What is the exception in java code? [closed]
Posted
by
Karandeep Singh
on Programmers
See other posts from Programmers
or by Karandeep Singh
Published on 2012-09-10T01:42:51Z
Indexed on
2012/09/10
3:48 UTC
Read the original article
Hit count: 408
java
This java code is for reverse the string but it returning concat null with returned string.
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Practice {
public static void main(String[] args) {
String str = "";
try {
str = reverse("Singh");
} catch (Exception ex) {
Logger.getLogger(Practice.class.getName()).log(Level.SEVERE, null, ex);
System.out.print(ex.getMessage());
}finally{
System.out.println(str);
}
}
public static String reverse(String str) throws Exception{
String temp = null;
if(str.length()<=0){
throw new Exception("empty");
}else{
for(int i=str.length()-1;i>=0;i--){
temp+=str.charAt(i);
}
}
return temp.trim();
}
}
Output:
nullhgniS
© Programmers or respective owner